package dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import dao.UserDao;
import domain.Message;
import domain.User;

public class UserDaoImpl implements UserDao {

	public boolean saveUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(user);
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}

	}

	public boolean updateUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(user);
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}
	}
	

	public boolean deleteUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(user);
			tx.commit();
			return true;
		} finally {
			if (s != null)
				s.close();
		}
	}


	public User findUserById(int id) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			User user = (User) s.get(User.class, id);
			return user;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public User findUserByName(String name) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from User as u where u.username=:name";
			List ul = s.createQuery(hql).setString("name", name).list();
			if(ul!=null && ul.size()>0){
				return (User)ul.get(0);
			}
			return null;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public User findUserByUsernameAndPassword(String username, String password) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "from User as u where u.username=:username and u.password=:password";
			List ul = s.createQuery(hql).setString("username", username).setString("password", password).list();
			if(ul!=null && ul.size()>0){
				return (User)ul.get(0);
			}
			return null;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public List findAllUserByPage(int pageNo,int pageSize) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			int offset=(pageNo-1)*pageSize;
			String hql = "from User as u order by u.id";
			return s.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public long findCount() {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "select count(u.id) from User as u";
			Object obj= s.createQuery(hql).uniqueResult();
			return (Long)obj;
		} finally {
			if (s != null)
				s.close();
		}
	}

	public void deleteById(int id)
    {
        Session session = null;
        Transaction tx = null;
        try
        {
            session = HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            User user = (User)session.get(User.class, Integer.valueOf(id));
            session.delete(user);
            tx.commit();
        }
        catch(HibernateException e)
        {
            e.printStackTrace();
        }
    }

}
